package com.ugrow.Study_System;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    Button btnRegister;
    EditText username,password,repassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
//        btnRegister = (Button) findViewById(R.id.btnRegister);
        username = (EditText) findViewById(R.id.nameEdit);
        password = (EditText) findViewById(R.id.password);
        repassword = (EditText) findViewById(R.id.repasswrod);
    }

    public void back(View view) {

        finish();

    }
    public void register1(View view){
        String usernameStr = username.getText().toString().trim();
        String passwordStr = password.getText().toString().trim();
        String repasswordStr = repassword.getText().toString().trim();

        if(!(TextUtils.isEmpty(usernameStr)||TextUtils.isEmpty(passwordStr)||TextUtils.isEmpty(repasswordStr))){
            //三个字符串都不为空
            if(passwordStr.length()<8||repasswordStr.length()<8){//密码长度不小于8位
                Toast.makeText(Register.this, "密码长度最少为8位", Toast.LENGTH_SHORT).show();
            }else {
                if (!passwordStr.equals(repasswordStr)) {//密码需要一致
                    Toast.makeText(Register.this, "两次密码输入不一致\n请重新输入", Toast.LENGTH_SHORT).show();
                    password.setText("");
                    repassword.setText("");
                }else{
                    //符合条件,允许注册,将用户名和密码存储到sp中
                    //获取sp对象 参数1:文件名 参数2 :存储模式,通常是私有化MODE_PRIVATE
                    SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
                    sp.edit().putString("USERNAME",usernameStr).putString("PASSWORD",passwordStr).commit();
                    finish();
                    Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();

                }
            }
        }else{
            //至少有一个为空,不允许注册,弹个土司提示一下
            Toast.makeText(Register.this, "请填写完整", Toast.LENGTH_SHORT).show();

        }
    }
}
